package com.sukaiyi.generator.config.utils;

import com.sukaiyi.generator.config.model.StructTemplate;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class TemplateUtils {

    public static MutableTreeNode toTreeNode(StructTemplate template) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(template);
        List<StructTemplate> children = template.getChildren();
        if(children != null && children.size() > 0){
            for (StructTemplate child : children) {
                node.add(toTreeNode(child));
            }
        }
        return node;
    }
    public static StructTemplate toStructTemplate(DefaultMutableTreeNode treeNode) {
        StructTemplate template = (StructTemplate) treeNode.getUserObject();
        Enumeration children = treeNode.children();
        List<StructTemplate> tplChildren = null;
        while (children.hasMoreElements()){
            DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement();
            if(tplChildren == null){
                tplChildren = new ArrayList<>();
            }
            tplChildren.add(toStructTemplate(child));
        }
        template.setChildren(tplChildren);
        return template;
    }
}
